package com.bike.bikeserver.common.utils;

import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/8/7 10:28
 */
public class ImageUtils
{
    /**
     * imgFile 图片本地存储路径
     */
    public static String getImgFileToBase64(String imgFile)
    {
        //将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream inputStream = null;
        byte[] buffer = null;
        //读取图片字节数组
        try
        {
            inputStream = new FileInputStream(imgFile);
            int count = 0;
            while (count == 0)
            {
                count = inputStream.available();
            }
            buffer = new byte[count];
            inputStream.read(buffer);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (inputStream != null)
            {
                try
                {
                    // 关闭inputStream流
                    inputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        // 对字节数组Base64编码
        return new BASE64Encoder().encode(buffer);
    }

}
